<?php
	
	require_once('classes/settings.php');
	require_once('classes/search.php');
	require_once('classes/tickets.php');
	require_once('classes/resultHandler.php');
	require_once('classes/filter.php');
	require_once('classes/pagination.php');
	require_once('classes/database.php');

	
	session_start();
	if(!isset($_SESSION['resulthandler'])){
		$_SESSION['resulthandler'] = new resultHandler($_SESSION['search']); 
		
	}
	
	if(!isset($_SESSION['filter'])){
            $_SESSION['filter'] = new Filter($_SESSION['allTickets']);   
	}
	
	
	if(isset($_GET['page'])){
		$results =  $_SESSION['resulthandler']->displayResults($_GET['page']); 
		require_once('includes/page.php');
	}
	
	if(isset($_GET['stop'])){
		if(count($_SESSION['stops']) == 0){
			$_SESSION['stops'] = $_SESSION['filter']->getStops();
			foreach($_SESSION['stops'] as $s => $k){
				$_SESSION['stops'][$s] = 1;
			}
			$_SESSION['stops'][$_GET['stop']] = 0;
		}else{
			if($_SESSION['stops'][$_GET['stop']]== 1){
				$_SESSION['stops'][$_GET['stop']] = 0;
			}else{
				$_SESSION['stops'][$_GET['stop']] = 1;
			}
		}
		//Disable stops
		 $x = $_SESSION['filter']->disableStops($_SESSION['stops']);
		 // new tickets
		 $_SESSION['resulthandler']->setTickets($x);
		 //weer naar pagina 1 met de gefilterde resultaten
		 $results =  $_SESSION['resulthandler']->displayResults(1); 
		 require_once('includes/page.php');
	}
	
	if($_GET['heenreis'] == true){
		$begintijd = $_GET['begintijd'];
		$terugtijd = $_GET['terugtijd'];
		
		$x = $_SESSION['filter']->betweenDepartureTime($begintijd,$terugtijd);
		$_SESSION['resulthandler']->setTickets($x);
		$results = $_SESSION['resulthandler']->displayResults(1); 
		
		require_once('includes/page.php');
		
	}
	
	if($_GET['terugreis'] == true){
		$begintijd = $_GET['begintijd'];
		$terugtijd = $_GET['terugtijd'];
		
		$x = $_SESSION['filter']->betweenReturnTime($begintijd,$terugtijd);
		$_SESSION['resulthandler']->setTickets($x);
		$results =  $_SESSION['resulthandler']->displayResults(1); 
		
		require_once('includes/page.php');
	}
	
	
	if(isset($_GET['carrier'])){
		if(count($_SESSION['carriers']) == 0){
			$_SESSION['carriers'] = $_SESSION['filter']->getCarriers();
			foreach($_SESSION['carriers'] as $s => $k){
				$_SESSION['carriers'][$s] = 1;
			}
			$_SESSION['carriers'][$_GET['carrier']] = 0;
		}else{
			if($_SESSION['carriers'][$_GET['carrier']]== 1){
				$_SESSION['carriers'][$_GET['carrier']] = 0;
			}else{
				$_SESSION['carriers'][$_GET['carrier']] = 1;
			}
		}
		
		//Disable stops
		$x =  $_SESSION['filter']->disableCarrier($_SESSION['carriers']);
		$_SESSION['resulthandler']->setTickets($x);
		 //weer naar pagina 1 met de gefilterde resultaten
		 $results =  $_SESSION['resulthandler']->displayResults(1); 
		 require_once('includes/page.php');
	}
	
	
		if($_GET['reistijd'] == true){
			$reistijd = $_GET['heentijd'];
			$maxtijd = $_GET['terugtijd'];
			
			$x = $_SESSION['filter']->travelTime($reistijd,$maxtijd);
			$_SESSION['resulthandler']->setTickets($x);
			$results = $_SESSION['resulthandler']->displayResults(1); 
			
			require_once('includes/page.php');
		
	}
	
?>